package com.dykj.fireFighting.web.devhis;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.base.dev.entity.DevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.entity.EsDevHisWater;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.service.EsDevHisWaterService;
import com.dykj.fireFighting.api.socialUnit.devHis.dto.DevHisFireDto;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.DevHisFire;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.SuspectedFire;
import com.dykj.fireFighting.api.socialUnit.devHis.service.IDevHisFireService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.Device;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 火灾自动报警系统设备运行记录
 *
 * @Author fgc
 * @Date 2021-06-15 16:52:12
 */
@Slf4j
@Api(value = "火灾自动报警系统设备运行记录接口", tags = {"火灾自动报警系统设备运行记录接口"})
@RestController
@RequestMapping("/devHis/devHisFire")
public class DevHisFireController {
    @DubboReference
    private IDevHisFireService devHisFireService;
    @Autowired
    private RedisService redisService;
    @DubboReference
    private EsDevHisService esDevHisService;
    @DubboReference
    private IDeviceService deviceService;
    /**
     * 火灾自动报警系统设备运行记录列表
     *
     * @return
     */
    @AutoLog(value = "火灾自动报警系统设备运行记录-分页列表查询")
    @ApiOperation(value = "火灾自动报警系统设备运行记录分页列表", notes = "火灾自动报警系统设备运行记录分页列表")
    @GetMapping("/list")
    @RequiresPermissions("devHis/devHisFire/list")
    public CommonResult<PageVo<EsDevHis>> treeGrid(DevHisFireDto devHisFireDto) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        devHisFireDto.setUnitId(enterpriseId + "");
        PageVo<EsDevHis> search = esDevHisService.search(enterpriseId + "", devHisFireDto.getName(),(int) devHisFireDto.getPage(), (int) devHisFireDto.getLimit(),
                devHisFireDto.getBeginDate(),devHisFireDto.getEndDate());
        return CommonResult.success(search);

    }

    /**
     * 获取火灾自动报警系统设备运行记录信息
     */
    @AutoLog(value = "火灾自动报警系统设备运行记录-获取VIEW信息")
    @ApiOperation(value = "获取火灾自动报警系统设备运行记录信息", notes = "获取火灾自动报警系统设备运行记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Long", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("devHis/devHisFire/info")
    public CommonResult<DevHisFire> info(@PathVariable("id") Long id) {
        DevHisFire devHisFire = devHisFireService.getById(id);
        return CommonResult.success(devHisFire);
    }
    @GetMapping("/getFires/{ids}")
    public CommonResult<DevHis>getByIds(@PathVariable("ids") String ids){
        DevHis devHis = JSON.parseObject((String) redisService.get(ids), DevHis.class);
        if(devHis==null){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                devHis=new DevHis();
                EsDevHis esDevHis= esDevHisService.getByDevHisId(ids);
                devHis.setId(esDevHis.getId());
                devHis.setRelationId(esDevHis.getRelationId());
                devHis.setZtfssj(sdf.parse(esDevHis.getZtfssj()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        Device device = JSON.parseObject((String) redisService.get(devHis.getRelationId()), Device.class);
        if(device==null){
            device=deviceService.getDeviceByRelationId(devHis.getRelationId());
        }
        devHis.setDeviceName(device.getName());
        return CommonResult.success(devHis);
    }
    @GetMapping("/getYsFires/{ids}")
    @Transactional
    public CommonResult<List<DevHisFire>>getYsFires(@PathVariable("ids") String ids){
        SuspectedFire suspectedFire = devHisFireService.getById(ids);
        List<DevHisFire> devHisFires = devHisFireService.queryByIds(suspectedFire.getDevHisIds());
        suspectedFire.setIsConfirm("1");
        devHisFireService.updateSuspectedFire(suspectedFire);
        return CommonResult.success(devHisFires);
    }


}
